package com.yelp.v2;

/*
 Example code based on code from Nicholas Smith at http://imnes.blogspot.com/2011/01/how-to-use-yelp-v2-from-java-including.html
 For a more complete example (how to integrate with GSON, etc) see the blog post above.
 */

import android.content.Context;

import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;

/**
 * Example for accessing the Yelp API.
 */
public class Yelp {

	OAuthService service;
	Token accessToken;

	public static Yelp getYelp() {

		String CONSUMER_KEY = "tORXcSu5PtltYIf6lTIssw";
		String CONSUMER_SECRET = "mk6wgHQbWxoJXMOx7gg5ccrHKYM";
		String TOKEN = "zu83Rt_oYXogF13nsEr3_7I6jXM74Yuv";
		String TOKEN_SECRET = "dcykGGJOagIDSqB4OEko7rxt7ss";

		return new Yelp(CONSUMER_KEY, CONSUMER_SECRET, TOKEN, TOKEN_SECRET);
	}

	/**
	 * Setup the Yelp API OAuth credentials.
	 * 
	 * OAuth credentials are available from the developer site, under Manage API
	 * access (version 2 API).
	 * 
	 * @param consumerKey
	 *            Consumer key
	 * @param consumerSecret
	 *            Consumer secret
	 * @param token
	 *            Token
	 * @param tokenSecret
	 *            Token secret
	 */
	public Yelp(String consumerKey, String consumerSecret, String token,
			String tokenSecret) {
		this.service = new ServiceBuilder().provider(YelpApi2.class)
				.apiKey(consumerKey).apiSecret(consumerSecret).build();
		this.accessToken = new Token(token, tokenSecret);
	}

	/**
	 * Search with term and location.
	 * 
	 * @param term
	 *            Search term
	 * @param latitude
	 *            Latitude
	 * @param longitude
	 *            Longitude
	 * @return JSON string response
	 */
	public String search(String term, double latitude, double longitude) {
		OAuthRequest request = new OAuthRequest(Verb.GET,
				"http://api.yelp.com/v2/search");
		request.addQuerystringParameter("term", term);
		request.addQuerystringParameter("lat", latitude + "");
		request.addQuerystringParameter("long", longitude + "");
		this.service.signRequest(this.accessToken, request);
		Response response = request.send();
		return response.getBody();
	}

	public String search(String term, double latitude, double longitude, double radius) {
		OAuthRequest request = new OAuthRequest(Verb.GET,
				"http://api.yelp.com/v2/search");
		request.addQuerystringParameter("term", term);
		request.addQuerystringParameter("lat", latitude + "");
		request.addQuerystringParameter("long", longitude + "");
		request.addQuerystringParameter("radius", radius + "");
		this.service.signRequest(this.accessToken, request);
		Response response = request.send();
		return response.getBody();
	}
	
	/**
	 * Search with term string location.
	 * 
	 * @param term
	 *            Search term
	 * @param latitude
	 *            Latitude
	 * @param longitude
	 *            Longitude
	 * @return JSON string response
	 */
	public String search(String term, String location) {
		OAuthRequest request = new OAuthRequest(Verb.GET,
				"http://api.yelp.com/v2/search");
		request.addQuerystringParameter("term", term);
		request.addQuerystringParameter("location", location);
		this.service.signRequest(this.accessToken, request);
		Response response = request.send();
		return response.getBody();
	}

//	// CLI
//	public static void main(String[] args) {
//		// Update tokens here from Yelp developers site, Manage API access.
//		String consumerKey = "";
//		String consumerSecret = "";
//		String token = "";
//		String tokenSecret = "";
//
//		Yelp yelp = new Yelp(consumerKey, consumerSecret, token, tokenSecret);
//		String response = yelp.search("burritos", 30.361471, -87.164326);
//
//		System.out.println(response);
//	}
}
